<?php

class RegistrationController extends Controller {

    public $defaultAction = 'registration';
    public $layout = '//layouts/column1';

    /**
     * Declares class-based actions.
     */
    public function actions() {
        return (isset($_POST['ajax']) && $_POST['ajax'] === 'registration-form') ? array() : array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor' => 0xFFFFFF,
            ),
                );
    }

    /**
     * Registration user
     */
    public function actionRegistration() {
        $model = new RegistrationForm;
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'registration-form') {
            echo UActiveForm::validate(array($model));
            Yii::app()->end();
        }
        
            if (isset($_POST['RegistrationForm'])) {
                $model->attributes = $_POST['RegistrationForm'];
                if ($model->validate()) {
                    $soucePassword = $model->password;
                    $model->salt = UserModule::getRandomSalt();
                    $hash1 = UserModule::activation($model->password);
                    $model->password = UserModule::encrypting($hash1.$model->salt);
                    $model->activation = UserModule::activation($model->salt);
                    $model->register_time = $model->last_login_time  = new CDbExpression('NOW()');
                    if ($model->save()) {
                        $identity = new UserIdentity($model->username, $soucePassword);
                        $identity->authenticate();
                        Yii::app()->user->login($identity, 0);
                        $this->redirect(Yii::app()->homeUrl);
                        $this->refresh();
                    }
                } 
            }
            $this->render('/user/registration', array('model' => $model));
        }

   
}